package com.rao.gmall.order.config;

import com.rao.gmall.common.config.MybatisPlusConfig;
import com.rao.gmall.common.handler.GlobalExceptionHandler;
import com.rao.gmall.feign.AppFeignConfig;
import com.rao.gmall.mq.AppRabbitConfig;
import feign.RequestInterceptor;
import lombok.Data;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author nzmxfan
 * @create 2021-08-20-19:38
 */
@AutoConfigureAfter(RedisAutoConfiguration.class)
@Configuration
@Import({AppFeignConfig.class, AppRabbitConfig.class , GlobalExceptionHandler.class, MybatisPlusConfig.class})
public class OrderConfig {

    // 将自定义类作为参数传入自定义的业务线程池，且将线程池命名为 corePool
    @Bean("corePool")
    public ThreadPoolExecutor executor(ThreadConfigProperties properties) {
        return new ThreadPoolExecutor(properties.core,
                properties.max,
                properties.keepalive,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(properties.queueLength),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }

    // 读取配置文件的属性并创建 set、get 方法
    @Data
    @Component
    @ConfigurationProperties(prefix = "order-service.thread")
    class ThreadConfigProperties {
        private int core;
        private int max;
        private Long keepalive;
        private int queueLength;
    }
}
